package demo.zyy.com.dreamstudio;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.aspsine.fragmentnavigator.FragmentNavigator;

import java.util.ArrayList;
import java.util.logging.Logger;

import demo.zyy.com.dreamstudio.base.BaseActivity;
import demo.zyy.com.dreamstudio.bottomnavigation.BottomNavigatorView;
import demo.zyy.com.dreamstudio.bottomnavigation.FragmentAdapter;
import demo.zyy.com.dreamstudio.bottomnavigation.broadcast.Action;
import demo.zyy.com.dreamstudio.bottomnavigation.broadcast.BroadcastManager;
import demo.zyy.com.dreamstudio.fragment.CarFragment;
import demo.zyy.com.dreamstudio.fragment.CategoryFragment;
import demo.zyy.com.dreamstudio.fragment.HomeFragment;
import demo.zyy.com.dreamstudio.fragment.MineFragment;

public class MainActivity extends BaseActivity implements BottomNavigatorView.OnBottomNavigatorViewItemClickListener {
    //默认位置
    private static final int DEFAULT_POSITION = 0;
    private FragmentNavigator mNavigator;
    private MenuItem mLoginMenu;
    private MenuItem mLogoutMenu;
    private BottomNavigatorView main_navigatorbutton;
    private ArrayList<Fragment> mFragmentlist=new ArrayList<>();

    @Override
    protected int loadXML() {
        return R.layout.activity_main;
    }
    @Override
    public void initParms(Bundle parms) {
        main_navigatorbutton = (BottomNavigatorView) findViewById(R.id.main_navigatorbutton);
        mFragmentlist.add(new HomeFragment());
        mFragmentlist.add(new CategoryFragment());
        mFragmentlist.add(new CarFragment());
        mFragmentlist.add(new MineFragment());
        mNavigator=new FragmentNavigator(getSupportFragmentManager(),new FragmentAdapter(mFragmentlist),R.id.main_content);
        mNavigator.setDefaultPosition(DEFAULT_POSITION);
        mNavigator.onCreate(parms);
        if(main_navigatorbutton!=null){
            main_navigatorbutton.setmOnBottomNavigatorViewItemClickListener(this);
        }
        setCurrentTab(mNavigator.getCurrentPosition());
        BroadcastManager.register(this,mLoginStatusChangeReceiver, Action.LOGIN,Action.LOGOUT);
    }
    @Override
    protected void initView(View view) {


    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {


    }

    @Override
    public void widgetClick(View v) {

    }


    @Override
    public void OnBNViewItemClick(int postion, View view) {
        setCurrentTab(postion);
    }
    private void setCurrentTab(int currentPosition) {
        mNavigator.showFragment(currentPosition);
        main_navigatorbutton.select(currentPosition);
    }
    private BroadcastReceiver mLoginStatusChangeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (!TextUtils.isEmpty(action)) {
                int position = intent.getIntExtra("EXTRA_POSITION", -1);
                if (action.equals(Action.LOGIN)) {
                    onUserLogin(position);
                } else if (action.equals(Action.LOGOUT)) {
                    onUserLogout(position);
                }
            }
        }
    };
    private void onUserLogout(int position) {
        if (position == -1) {
            resetAllTabsAndShow(mNavigator.getCurrentPosition());
        } else {
            resetAllTabsAndShow(position);
        }
        toggleMenu(false);
    }
    private void onUserLogin(int position) {
        if (position == -1) {
            resetAllTabsAndShow(mNavigator.getCurrentPosition());
        } else {
            resetAllTabsAndShow(position);
        }
        toggleMenu(true);
    }
    private void resetAllTabsAndShow(int position){
        mNavigator.resetFragments(position, true);
        main_navigatorbutton.select(position);
    }
    private void toggleMenu(boolean login) {
        if (login) {
            mLoginMenu.setVisible(false);
            mLogoutMenu.setVisible(true);
        } else {
            mLoginMenu.setVisible(true);
            mLogoutMenu.setVisible(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BroadcastManager.unregister(this, mLoginStatusChangeReceiver);
    }
}
